package com.bsh.repository;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.bsh.utils.Pager;

/** 
 *
 *
 * @author  kms 
 * @version 1.0  
 */
@Repository
public class DaoHOrder {
	@Resource
	SqlSessionTemplate session;

	@Resource
	JdbcTemplate jdbcTemplate;
	@Resource
	NamedParameterJdbcTemplate namedJdbcTemplate;
	
	
	/**
	 * 账户列表
	 */
	public List<Map<String,Object>> getPage(Pager pager,String where,String order) {				
		StringBuilder sb=new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();		
		sb.append(" select o.id,m.merchant_name,o.order_num,od.ammount,od.is_first,od.is_paid,o.status,hr.name,od.real_name, ");
		sb.append(" DATE_FORMAT(od.in_time,'%Y-%m-%d') as in_time,DATE_FORMAT(od.out_time,'%Y-%m-%d') as out_time,od.num from h_order o ");
		sb.append(" LEFT JOIN h_order_detail od ON  od.order_id=o.id ");
		sb.append(" LEFT JOIN h_order_room  hr on hr.id=od.room_id ");	
		sb.append(" LEFT JOIN h_hotel hh on hh.id=hr.hotel_id ");
		sb.append(" LEFT JOIN merchant m on m.id=hh.merchant_id ");
		sb.append(" where 1=1 ");
		if(!StringUtils.isEmpty(where)){
			sb.append(where);
		}
		Integer count= namedJdbcTemplate.queryForInt("select count(0) from ("+sb.toString()+") v",ps);

		StringBuilder sb2=new StringBuilder();
		MapSqlParameterSource ps2 = new MapSqlParameterSource();
		sb2.append(sb.toString());
		sb2.append(" order by ");
		if(!StringUtils.isEmpty(order)){			
			sb2.append(order+",");
		}
		sb2.append(" o.create_time desc ");
		sb2.append(" limit :start,:size");		
		ps2.addValue("start", pager.getStart());
		ps2.addValue("size", pager.getPageSize());
		List<Map<String,Object>> list=namedJdbcTemplate.queryForList(sb2.toString(), ps2);

		pager.setRowCount(count);
		return list;
	}


}
